import wx
from wx.lib.rcsizer import RowColSizer
import co

ADD=wx.NewId()
CANCEL=wx.NewId()


class addperson(wx.Dialog):
    def __init__(self,parent,id,title,edit,idu,**kwds):
        
        #initialisation
        self.connect = co.co()
        self.title = title
        self.edit = edit
        self.idu = 0
        self.parms = []
        self.params = ['','','','','','','','','']
        tabPostes = self.connect.getpostes()
        tabServices = self.connect.getservices()
        if edit:
            self.parms = self.connect.getpersonparms(idu)
            i = 0
            for elem in self.parms:
                self.params[i] = elem
                i+=1
            self.idu = idu
            
        wx.Dialog.__init__(self,parent,id,title,**kwds)
        
        #create the widgets
        
        #self.nomEmp = wx.RadioBox(self, -1,"Salutation: ", wx.DefaultPosition,wx.DefaultSize,tabPostes,1,wx.RA_SPECIFY_ROWS)
        #self.nomEmp.SetStringSelection(str(self.parms['Nom']))
        
        self.labNom = wx.StaticText(self, -1, "Nom : ")
        self.txtNom = wx.TextCtrl(self, -1, str(self.params[1]))
        self.labPrenom = wx.StaticText(self, -1, "Prenom :")
        self.txtPrenom = wx.TextCtrl(self, -1, str(self.params[2]))
        self.labAge = wx.StaticText(self, -1, "Age : ")
        self.txtAge = wx.TextCtrl(self, -1, str(self.params[3]))
        self.labSalaire = wx.StaticText(self, -1, "Salaire :")
        self.txtSalaire = wx.TextCtrl(self, -1, str(self.params[4]))
        self.labServices = wx.StaticText(self, -1,"Services :")
        self.comboServices = wx.ComboBox(self, -1, choices=tabServices,style=wx.CB_DROPDOWN)
        self.comboServices.SetValue(str(self.params[6]))
        self.labPostes = wx.StaticText(self, -1,"Poste :")
        self.comboPostes = wx.ComboBox(self, -1, choices=tabPostes,style=wx.CB_DROPDOWN)
        self.comboPostes.SetValue(str(self.params[8]))
        self.butsave = wx.Button(self, ADD, "Save")
        self.butcancel = wx.Button(self, CANCEL, "Cancel")
        #self.txtNom.SetSize((320, 26))
        #self.txtNom.SetMaxLength(40)
        #self.txtPrenom.SetSize((320, 26))
        #self.txtPrenom.SetMaxLength(40)
        self.__do_layout()


    def __do_layout(self):
        
        self.SetPosition([200,200])
        boxl = RowColSizer()
        boxl.Add(self.labNom, row=1, col=1)
        boxl.Add(self.txtNom, row=1, col=2)
        boxl.Add(self.labPrenom, row=1, col=3)
        boxl.Add(self.txtPrenom, row=1, col=4)
        boxl.Add(self.labAge, row=2, col=1)
        boxl.Add(self.txtAge, row=2, col=2)
        boxl.Add(self.labSalaire, row=2, col=3)
        boxl.Add(self.txtSalaire, row=2, col=4 )
        boxl.Add(self.labServices, row=3, col=1)
        boxl.Add(self.comboServices, row=3, col=2)
        boxl.Add(self.labPostes, row=4, col=1)
        boxl.Add(self.comboPostes, row=4, col=2)
       
        
        boxb = wx.BoxSizer(wx.HORIZONTAL)
        boxb.Add(self.butsave, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
        boxb.Add(self.butcancel, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
        boxl.Add(boxb, row=5, col=2)
        
        for x in range(1,5):
            boxl.AddSpacer(75,30, pos=(x,1))
            boxl.AddSpacer(150,1, pos=(x,2))
            
        boxl.AddSpacer(75,30,pos=(5,1))
        self.SetAutoLayout(1)
        self.SetSizer(boxl)
        boxl.Fit(self)
        boxl.SetSizeHints(self)
        self.Layout()
        wx.EVT_BUTTON(self, ADD, self.add)
        wx.EVT_BUTTON(self, CANCEL, self.cancel)
 
    def cancel(self,event):
        self.EndModal(0)
        
    def add(self, event):
        msg=''
        ok= True
        self.params[1] = self.txtNom.GetValue()
        self.params[2] = self.txtPrenom.GetValue()
        self.params[3] = self.txtAge.GetValue()
        self.params[4] = self.txtSalaire.GetValue()
        self.params[6] = self.comboServices.GetStringSelection()
        self.params[8] = self.comboPostes.GetStringSelection()
        #if edit mode
        if self.edit:
            #check for duplicates
            self.params[0] = self.idu
            if self.params[8] != '' : self.params[7] = self.connect.getidposte(self.params[8])
            if self.params[6] != '' : self.params[5] = self.connect.getidservice(self.params[6])
            msg,ok = self.connect.duplicedit(self.params[1], self.params[2],self.idu,msg,ok)
            if ok:
                if self.connect.updateperson(self.params,self.idu)==1:
                    self.EndModal(1)
            else:
                    #display error message
                error = wx.MessageDialog(self,msg,'Error',wx.OK)
                error.ShowModal()
                error.Destroy()    
        #if add mode    
        else:
            self.params[7] = self.connect.getidposte(self.params[8])
            self.params[5] = self.connect.getidservice(self.params[6])
            msg,ok = self.connect.duplic(self.params[1],self.params[2],msg,ok)
            if ok:
                self.connect.enterperson(self.params)
                self.EndModal(1)
            else:
                error = wx.MessageDialog(self,msg,'Error',wx.OK)
                error.ShowModal()
                error.Destroy()
